<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "company".
 *
 * @property integer $id_company
 * @property string $name
 * @property string $address
 * @property string $email
 * @property string $phone
 * @property integer $status
 * @property string $created
 * @property string $updated
 * @property integer $id_package
 * @property string $expiration
 *
 * @property Attribute[] $attributes
 * @property Branch[] $branches
 * @property CategoryCompany[] $categoryCompanies
 * @property Group[] $groups
 * @property Import[] $imports
 * @property Manufacture[] $manufactures
 * @property Product[] $products
 * @property User[] $users
 */
class Company extends \yii\db\ActiveRecord
{
    
    
    
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'company';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'address', 'email', 'phone'], 'required'],
            [['status', 'id_package'], 'integer'],
            ['email', 'email'],
            ['email', 'unique'],
            [['created', 'updated', 'expiration'], 'safe'],
            [['name', 'address', 'email', 'phone'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id_company' => 'Id Company',
            'name' => 'Tên cty',
            'address' => 'Đại chỉ',
            'email' => 'Email',
            'phone' => 'Số dt',
            'status' => 'Status',
            'created' => 'Created',
            'updated' => 'Updated',
            'id_package' => 'Id Package',
            'expiration' => 'Expiration',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getAttributes()
    {
        return $this->hasMany(Attribute::className(), ['id_company' => 'id_company']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getBranches()
    {
        return $this->hasMany(Branch::className(), ['id_company' => 'id_company']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCategoryCompanies()
    {
        return $this->hasMany(CategoryCompany::className(), ['id_company' => 'id_company']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getGroups()
    {
        return $this->hasMany(Group::className(), ['id_company' => 'id_company']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getImports()
    {
        return $this->hasMany(Import::className(), ['id_company' => 'id_company']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getManufactures()
    {
        return $this->hasMany(Manufacture::className(), ['id_company' => 'id_company']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getProducts()
    {
        return $this->hasMany(Product::className(), ['id_company' => 'id_company']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUsers()
    {
        return $this->hasMany(User::className(), ['id_company' => 'id_company']);
    }
    
    /**
     * 
     */
    public function beforeSave($insert) {
        parent::beforeSave($insert);
        if($insert){
            $this->created = date('Y-m-d');
        }
        $this->updated = date('Y-m-d');
        return true;
    }
}
